package com.sixbro.shiro.uaa;

import com.sixbro.shiro.uaa.filter.CustomFilterChainDefinition;
import com.sixbro.shiro.uaa.filter.CustomFilterWrapper;
import com.sixbro.shiro.uaa.filter.ResourceServerFilterChainDefinition;
import com.sixbro.shiro.uaa.filter.ResourceServerFilterWrapper;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 18:03
 */
@EnableConfigurationProperties(ResourceServerProperties.class)
@AutoConfigureBefore({ShiroFilterAutoConfiguration.class, ShiroAutoConfiguration.class})
@ConditionalOnClass(Realm.class)
public class ResourceServerFilterAutoConfiguration {

    @Autowired
    private ResourceServerProperties resourceProperties;

    @Value("${shiro.logoutUrl}")
    private String logoutUrl;

    @Bean
    public CustomFilterWrapper oauth2ResourceFilter() {
        ResourceServerFilterWrapper filter = new ResourceServerFilterWrapper();
        filter.setServerScheme(resourceProperties.getServerScheme());
        filter.setServerHost(resourceProperties.getServerHost());
        filter.setServerPort(resourceProperties.getServerPort());
        filter.setServerContextPath(resourceProperties.getServerContextPath());
        filter.setClientId(resourceProperties.getClientId());
        filter.setClientSecret(resourceProperties.getClientSecret());
        return filter;
    }

    @Bean
    public CustomFilterChainDefinition oauth2ResourceFilterChain() {
        return new ResourceServerFilterChainDefinition(logoutUrl);
    }
}